/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Interfaz.java
 *
 * Created on 3/09/2010, 06:47:39 PM
 */
package smartrobot;

import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo Velez
 */
public class Interfaz extends javax.swing.JFrame {

    /** Creates new form Interfaz */
    int mundo[][] = null;
    JLabel[][] labels = new JLabel[10][10];
    private ArrayList ruta;
    Hilo l;

    public Interfaz() {
        super("UVSmartRobot");
        initComponents();
        pnContenedor.setLayout(new GridLayout(10, 10));
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                labels[i][j] = new JLabel("");
                labels[i][j].setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 1));
                pnContenedor.add(labels[i][j]);
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(null);

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel2.setLayout(null);

        btnMover.setText("Moverse");
        btnMover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoverActionPerformed(evt);
            }
        });
        jPanel2.add(btnMover);
        btnMover.setBounds(170, 570, 110, 23);

        pnContenedor.setBackground(new java.awt.Color(255, 255, 255));
        pnContenedor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        pnContenedor.setLayout(null);
        jPanel2.add(pnContenedor);
        pnContenedor.setBounds(50, 40, 590, 510);

        btnDetener.setText("Detener");
        btnDetener.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetenerActionPerformed(evt);
            }
        });
        jPanel2.add(btnDetener);
        btnDetener.setBounds(310, 570, 110, 23);

        jTabbedPane1.addTab("Mundo", jPanel2);

        jPanel3.setLayout(null);

        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel4.setLayout(new java.awt.BorderLayout());

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel4);
        jPanel4.setBounds(40, 30, 480, 540);

        jTabbedPane1.addTab("Informes", jPanel3);

        jPanel1.add(jTabbedPane1);
        jTabbedPane1.setBounds(40, 10, 700, 640);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jMenu1.setText("Mundo");

        jMenuItem1.setText("Ingresar Mundo");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Busquedas");

        jMenu3.setText("No Informadas");

        jMenuItem2.setText("Por Amplitud");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("Costo Uniforme");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem4.setText("Por Profundidad");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenu2.add(jMenu3);

        jMenu4.setText("Informadas");

        jMenu5.setText("Avara");

        MnHAvara1.setText("Heuristica 1");
        MnHAvara1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MnHAvara1ActionPerformed(evt);
            }
        });
        jMenu5.add(MnHAvara1);

        MnHAvara2.setText("Heuristica 2");
        MnHAvara2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MnHAvara2ActionPerformed(evt);
            }
        });
        jMenu5.add(MnHAvara2);

        jMenu4.add(jMenu5);

        jMenu6.setText("A*");

        MnHAStar1.setText("Heuristica 1");
        MnHAStar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MnHAStar1ActionPerformed(evt);
            }
        });
        jMenu6.add(MnHAStar1);

        MnHAstar2.setText("Heuristica 2");
        MnHAstar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MnHAstar2ActionPerformed(evt);
            }
        });
        jMenu6.add(MnHAstar2);

        jMenu4.add(jMenu6);

        jMenu2.add(jMenu4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-769)/2, (screenSize.height-731)/2, 769, 731);
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        try {
            // TODO add your handling code here:
            mundo = leerMundo();
            llenarGrilla(mundo);
        } catch (IOException ex) {
            Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:

        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaPA b1 = new BusquedaPA();
            b1.realizarBusqueda();
            setRuta(b1.getCamino());



        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaCU b = new BusquedaCU();
            b.realizarBusqueda();
            setRuta(b.getCamino());


        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaPP b = new BusquedaPP();
            b.realizarBusqueda();
            setRuta(b.getCamino());

        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void btnMoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoverActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
        llenarGrilla(mundo);
        l = new Hilo(getRuta());
        l.iniciar();
        }

    }//GEN-LAST:event_btnMoverActionPerformed

    private void btnDetenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetenerActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
        l.detener();
        }
    }//GEN-LAST:event_btnDetenerActionPerformed

    private void MnHAvara1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MnHAvara1ActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaAvara b = new BusquedaAvara(0);
            b.realizarBusqueda();
            setRuta(b.getCamino());

        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }



    }//GEN-LAST:event_MnHAvara1ActionPerformed

    private void MnHAStar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MnHAStar1ActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaAStar b = new BusquedaAStar(0);
            b.realizarBusqueda();
            setRuta(b.getCamino());

        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }

    }//GEN-LAST:event_MnHAStar1ActionPerformed

    private void MnHAvara2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MnHAvara2ActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaAvara b = new BusquedaAvara(1);
            b.realizarBusqueda();
            setRuta(b.getCamino());

        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }
    }//GEN-LAST:event_MnHAvara2ActionPerformed

    private void MnHAstar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MnHAstar2ActionPerformed
        // TODO add your handling code here:
        if (mundo != null) {
            Espacio.setEspacio(mundo);
            BusquedaAStar b = new BusquedaAStar(1);
            b.realizarBusqueda();
            setRuta(b.getCamino());

        } else {
            JOptionPane.showMessageDialog(this, "Por favor primero ingrese el mundo");
        }
    }//GEN-LAST:event_MnHAstar2ActionPerformed
    /**
     * @param args the command line arguments
     */
    /* public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new Interfaz().setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static final javax.swing.JMenuItem MnHAStar1 = new javax.swing.JMenuItem();
    public static final javax.swing.JMenuItem MnHAstar2 = new javax.swing.JMenuItem();
    public static final javax.swing.JMenuItem MnHAvara1 = new javax.swing.JMenuItem();
    public static final javax.swing.JMenuItem MnHAvara2 = new javax.swing.JMenuItem();
    public static final javax.swing.JButton btnDetener = new javax.swing.JButton();
    public static final javax.swing.JButton btnMover = new javax.swing.JButton();
    public static final javax.swing.JMenu jMenu1 = new javax.swing.JMenu();
    public static final javax.swing.JMenu jMenu2 = new javax.swing.JMenu();
    public static final javax.swing.JMenu jMenu3 = new javax.swing.JMenu();
    public static final javax.swing.JMenu jMenu4 = new javax.swing.JMenu();
    public static final javax.swing.JMenu jMenu5 = new javax.swing.JMenu();
    public static final javax.swing.JMenu jMenu6 = new javax.swing.JMenu();
    public static final javax.swing.JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
    public static final javax.swing.JMenuItem jMenuItem1 = new javax.swing.JMenuItem();
    public static final javax.swing.JMenuItem jMenuItem2 = new javax.swing.JMenuItem();
    public static final javax.swing.JMenuItem jMenuItem3 = new javax.swing.JMenuItem();
    public static final javax.swing.JMenuItem jMenuItem4 = new javax.swing.JMenuItem();
    public static final javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
    public static final javax.swing.JPanel jPanel2 = new javax.swing.JPanel();
    public static final javax.swing.JPanel jPanel3 = new javax.swing.JPanel();
    public static final javax.swing.JPanel jPanel4 = new javax.swing.JPanel();
    public static final javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
    public static final javax.swing.JTabbedPane jTabbedPane1 = new javax.swing.JTabbedPane();
    public static final javax.swing.JTextArea jTextArea1 = new javax.swing.JTextArea();
    public static final javax.swing.JPanel pnContenedor = new javax.swing.JPanel();
    // End of variables declaration//GEN-END:variables

    public int[][] leerMundo() throws IOException {
        String text = "";
        int arreglo[][] = new int[10][10];
        JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
        fc.showOpenDialog(this);
        File Abrir = fc.getSelectedFile(); //Devuelve el File que vamos a leerName
        if (Abrir != null) {
            FileReader Fichero = null;
            try {
                Fichero = new FileReader(Abrir);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedReader leer = new BufferedReader(Fichero);

            try {

                for (int i = 0; ((text = leer.readLine()) != null); i++) {
                    text = text.replace(" ", "");

                    for (int j = 0; j < 10; j++) {
                        arreglo[i][j] = Integer.parseInt(String.valueOf(text.charAt(j)));
                    }
                }

            } catch (IOException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            }
            leer.close();

        }
        return arreglo;
    }

    public void llenarGrilla(int[][] mundo) {


        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {

                if (mundo != null) {
                    if (mundo[i][j] == 0) {
                        labels[i][j].setIcon(iconos[7]);
                    }
                    if (mundo[i][j] == 1) {
                        labels[i][j].setIcon(iconos[1]);
                    }
                    if (mundo[i][j] == 2) {
                        labels[i][j].setIcon(iconos[0]);
                    }
                    if (mundo[i][j] == 3) {
                        labels[i][j].setIcon(iconos[2]);
                    }
                    if (mundo[i][j] == 4) {
                        labels[i][j].setIcon(iconos[5]);
                    }
                    if (mundo[i][j] == 5) {
                        labels[i][j].setIcon(iconos[6]);
                    }
                    if (mundo[i][j] == 6) {
                        labels[i][j].setIcon(iconos[4]);
                    }
                    if (mundo[i][j] == 7) {
                        labels[i][j].setIcon(iconos[3]);
                    }
                    pnContenedor.add(labels[i][j]);
                }
            }
        }
    }

    /**
     * @return the ruta
     */
    public ArrayList getRuta() {
        return ruta;
    }

    /**
     * @param ruta the ruta to set
     */
    public void setRuta(ArrayList ruta) {
        this.ruta = ruta;
    }

    public void moverPanel() {

        for (int i = 0; i < 1000; i++) {

            pnContenedor.setBounds(50, 40, 590, 510);
            pnContenedor.setBounds(53, 40, 590, 510);
            pnContenedor.setBounds(56, 40, 590, 510);
            pnContenedor.setBounds(59, 40, 590, 510);
            pnContenedor.setBounds(62, 40, 590, 510);
            pnContenedor.setBounds(65, 40, 590, 510);
            pnContenedor.setBounds(62, 40, 590, 510);
            pnContenedor.setBounds(59, 40, 590, 510);
            pnContenedor.setBounds(56, 40, 590, 510);
            pnContenedor.setBounds(53, 40, 590, 510);
            pnContenedor.setBounds(50, 40, 590, 510);
            pnContenedor.setBounds(47, 40, 590, 510);
            pnContenedor.setBounds(44, 40, 590, 510);
            pnContenedor.setBounds(41, 40, 590, 510);
            pnContenedor.setBounds(39, 40, 590, 510);
            pnContenedor.setBounds(41, 40, 590, 510);
            pnContenedor.setBounds(44, 40, 590, 510);
            pnContenedor.setBounds(47, 40, 590, 510);
            pnContenedor.setBounds(50, 40, 590, 510);
        }




    }

    public class Hilo implements Runnable {

        //private boolean continuar = true;
        ArrayList<int[]> camino;

        public Hilo(ArrayList<int[]> _camino) {
            camino = _camino;
        }
        // metodo para poner el boolean a false.
        private Thread animationThread = null;

        public void run() {
            Thread currentThread = Thread.currentThread();
            int i = 0;

            while (currentThread == animationThread && i < camino.size()) {
                //Logica del hilo

                if (i > 0) {


                    if(labels[camino.get(i)[0]][camino.get(i)[1]].getIcon().equals(iconos[3])){


                        if(labels[camino.get(i-1)[0]][camino.get(i-1)[1]].getIcon().equals(iconos[0])||labels[camino.get(i-1)[0]][camino.get(i-1)[1]].getIcon().equals(iconos[4])){
                        labels[camino.get(i-1)[0]][camino.get(i-1)[1]].setIcon(iconos[7]);
                        }else{
                        labels[camino.get(i-1)[0]][camino.get(i-1)[1]].setIcon(iconos[3]);
                        }
                        moverPanel();
                        i++;
                    }else{
                    labels[camino.get(i - 1)[0]][camino.get(i - 1)[1]].setIcon(iconos[7]);////elimina el robot en la posicion anterior
                    labels[camino.get(i)[0]][camino.get(i)[1]].setIcon(iconos[0]);
                    }
                    

                    /*if(labels[camino.get(i)[0]][camino.get(i)[1]].getIcon().equals(iconos[3])){
                    labels[camino.get(i - 1)[0]][camino.get(i - 1)[1]].setIcon(iconos[7]);
                    moverPanel();
                    }else{
                    labels[camino.get(i - 1)[0]][camino.get(i - 1)[1]].setIcon(iconos[7]);////elimina el robot en la posicion anterior
                    labels[camino.get(i)[0]][camino.get(i)[1]].setIcon(iconos[0]);/////pone el robot en la coordenada dada
                    }*/
                }
                i++;

                try {

                    animationThread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }

        public void iniciar() {
            if (this.animationThread == null) {
                this.animationThread = new Thread(this, "animacion");
                this.animationThread.start();
            }

        }

        public void detener() {
            this.animationThread = null;
            llenarGrilla(mundo);

        }

        public void pausar() {
            this.animationThread = null;
        }
    }
    public Icon[] iconos = {new javax.swing.ImageIcon(getClass().getResource("/icons/misc051.gif")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/muro.png")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/salida.png")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/campo.jpg")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/itembattery.jpg")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/nave1.png")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/nave2.gif")),
        new javax.swing.ImageIcon(getClass().getResource("/icons/blanco.png"))};
}


